class Counter {
  constructor(limit) {
    this.limit = limit;
  }

  [Symbol.iterator]() {
    let count = 1;
    let limit = this.limit;
    return {
      next() {
        if (count <= limit) {
          return { done: false, value: count++ };
        } else {
          return { done: true, value: undefined };
        }
      },
      return() {
        console.log("Exiting early");
        return { done: true };
      },
    };
  }
}

let counter1 = new Counter(5);
for (let i of counter1) {
  console.log(i);
}

for (let i of counter1) {
  if (i > 2) {
    break;
  }
  console.log(i)
}


let counter2 = new Counter(5)
try {
  for (let i of counter2) {
    if (i > 2) {
      throw 'err'
    }
    console.log(i)
  }
} catch (e) { }


let counter3 = new Counter(5)
let [a,b] = counter3
